﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DonViNganh
{
    public class DoanSinh
    {
        private DonViNganhDataContext dbDoanSinh = null;

        #region Them,Sua,Xoa
        /// <summary>
        /// Thêm mới đoàn sinh
        /// </summary>
        /// <param name="doansinh"></param>
        /// <param name="taikhoan"></param>
        /// <param name="madonvi"></param>
        /// <param name="madoansinh"></param>
        /// <param name="filename"></param>
        public void themMoiDoanSinh(DOANSINH doansinh,string taikhoan,Int32 madonvi, Int64 madoansinh, string filename)
        {
            dbDoanSinh = new DonViNganhDataContext();
            try
            {

                dbDoanSinh.DOANSINHs.InsertOnSubmit(doansinh);

                // them hinh anh neu co
                if (filename.Length !=0)
                {
                    DOANSINH_HINHANH hinhanh = new DOANSINH_HINHANH();
                    hinhanh.MADOANSINH = madoansinh;
                    hinhanh.NGAYTAO = DateTime.Now.Date;
                    dbDoanSinh.DOANSINH_HINHANHs.InsertOnSubmit(hinhanh);
                }

                // them moi vao so sinh hoat
                DOANSINH_SINHHOAT sinhhoat = new DOANSINH_SINHHOAT();
                sinhhoat.MADOANSINH = madoansinh;
                sinhhoat.MADONVIID = madonvi;
                sinhhoat.NGAYVAODONVI = DateTime.Now.Date;
                sinhhoat.NGAYTAO = DateTime.Now.Date;
                sinhhoat.TAIKHOANTAO = taikhoan;
                sinhhoat.HOATDONG=false;
                dbDoanSinh.DOANSINH_SINHHOATs.InsertOnSubmit(sinhhoat);

                dbDoanSinh.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Xoa doan sinh
        /// </summary>
        /// <param name="madoansinh"></param>
        public void xoaDoanSinh(Int64 madoansinh)
        {
            dbDoanSinh = new DonViNganhDataContext();
            try
            {
                string sql = "UPDATE QUANTRI.DOANSINH_SINHHOAT SET HOATDONG = 'False' WHERE MADOANSINH = " + madoansinh;
                dbDoanSinh.ExecuteQuery<DOANSINH_SINHHOAT>(sql);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        
        #endregion

        #region DonVi

        public List<Model.DoanPhaDonVi> getDoanPhaDonVi(Int32 MaDonVi)
        {

            List<Model.DoanPhaDonVi> lst = new List<Model.DoanPhaDonVi>();
            dbDoanSinh = new DonViNganhDataContext();
            var qu = dbDoanSinh.sp_getdanhsachdoansinh(MaDonVi);
            foreach (var item in qu.ToList())
            {
                Model.DoanPhaDonVi itemDetails = new Model.DoanPhaDonVi();
                itemDetails.MaDoanSinh = (long)item.MADOANSINH;
                itemDetails.HoVaTen = item.HOLOT + " " + item.TEN;
                itemDetails.NgaySinh = item.NGAYSINH.Value.ToShortDateString();
                itemDetails.NoiSinh = item.NOISINH;
                itemDetails.DiaChi = item.DIACHI;
                itemDetails.SoDTNha = item.SODIENTHOAI;
                itemDetails.SoDTDD = item.SODIENTHOAIDD;
                itemDetails.TenDonVi = item.TENDONVI;
                itemDetails.GioiTinh = item.PHAI;
                itemDetails.Email = item.EMAIL;
                itemDetails.DaTuyenHua = item.DATUYENHUA.ToString();
                itemDetails.HoTenCha = item.HOTENCHA;
                itemDetails.SoDTCha = item.SODTCHA;
                itemDetails.HoTenMe = item.HOTENME;
                itemDetails.SoDTMe = item.SODTME;
                itemDetails.TenDonVi = item.TENDONVI;
                itemDetails.NgayTuyenHua = item.NGAYTUYENHUA;
                itemDetails.NoiTuyenHua = item.TENDONVITUYENHUA;
                itemDetails.socmnd = item.SOCMND;
                itemDetails.ngaycap = item.NGAYCAPCMND != null?item.NGAYCAPCMND:"";
                itemDetails.noicap = item.NOICAP;
                lst.Add(itemDetails);
            }
            return lst;
        }
        #endregion
    }
}
